<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\market\controller;


use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\facade\Arr;
/**
 * 二手产品
 * Class Index
 * @package app\admin\controller
 */
class Market extends Controller
{
    public $table = 'Market';


    /**
     * 商品概况
     * @auth true
     * @menu true
     */
    public function index()
    {   
       
       //获取当前代理商所在城市

        $data['total_market']=DB::name($this->table)->count();
        $data['sucess_market']=DB::name($this->table)->where('status=3 ')->count();
        $data['pre_market']=DB::name($this->table)->where('status=2')->count();
        $data['deny_market']=DB::name($this->table)->where('status=4')->count();

        $this->assign('result',$data);

        $this->fetch();

    }

  



    /**
    * 商品列表
    * @auth true
    *@menu true
    */
    public function marketList()
    {   

        $this->title = '商品列表';

        $status = $this->request->param('status',-1);
        
        if($status=='-1'){
            $data[]=['status','IN',[2,3,4]];
        }else{
            $data[]=['status','EQ',$status];
            if($status==2){
                $this->title = '待审核商品';
            }elseif ($status==3) {
                $this->title = '审核通过商品';
            }elseif ($status==4) {
                $this->title = '审核拒绝商品';
            }else{
                $data[]=['status','IN',[2,3,4]];
            }
        }
       
        $markettype=DB::name("MarketType")->field("id,name")->select();
        $this->assign('markettype',$markettype);

        $query = $this->_query($this->table)
            ->where($data)
            ->like('name')
            ->equal('id,name,tid,status,is_show');
        $query->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _marketList_page_filter(&$data)
    {


        $this->clist = Db::name('MarketType')->field("id,name")->select();
        foreach ($data as &$vo) {

            $vo['marketstatus'] = TransItem::marketStatus(Arr::get($vo, 'status'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['add_time'] = date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['tid']) $vo['cate'] = $cate;

        }
    }

     /**
    * 商品详情
    * @auth true
    */
    public function edit(){
          
            $this->title = '商品详情';
           
            //商品类型
            $marktype = Db::name('MarketType')
                    ->where('status=1')
                    ->field("id,name")
                    ->select();
            $this->assign('marktype',$marktype);

           

            $id = $this->request->param('id', 0);
            //$result=DB::name($this->table)->where('id',$id)->find();
            $result=DB::name($this->table)
                    ->alias('m')
                    ->join('MarketExtend me','m.id=me.market_id')
                    ->where('m.id',$id)
                    ->field("m.*,me.content,me.id as markid")
                    ->find();
            $result['add_time']=date("Y-m-d",$result['created_at']);
            $this->assign('result',$result);



             //获取代理商所在省份城市
            $address['province']=DB::name("Area")->where('area_id',$result['provinces_id'])->value('area_name');
            $address['city']=DB::name("Area")->where('area_id',$result['city_id'])->value('area_name');
            $address['county']=DB::name("Area")->where('area_id',$result['county_id'])->value('area_name');
            $this->assign('address',$address);

            $this->fetch();
        
    }


  

    


    



}
